package com.tbit.uqbike.client.config;

import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @Author: chen
 * @Description: 分布式定时任务锁redis连接
 * @Date: 创建于 17:44 2022/7/13
 */
@Configuration
//defaultLockAtMostFor 指定在执行节点结束时应保留锁的默认时间使用ISO8601 Duration格式
//作用就是在被加锁的节点挂了时，无法释放锁，造成其他节点无法进行下一任务
//这里55s
@EnableSchedulerLock(defaultLockAtMostFor = "PT55S")
public class ShedlockConfig {

    //redis配置文件
    @Autowired
    private RedisTemplate redisTemplate;

    @Bean
    public LockProvider lockProvider() {
        return new RedisLockProvider(redisTemplate.getConnectionFactory());
    }
}
